package com.tsingteng.cosfun.ui.message.comment;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.base.BasePresenterFragment;
import com.tsingteng.cosfun.bean.CenterThreeNotification;
import com.tsingteng.cosfun.bean.PlayDetailsBean;
import com.tsingteng.cosfun.common.Navigate;
import com.tsingteng.cosfun.helper.VideoHelper;
import com.tsingteng.cosfun.ui.cosfun.PlayActivity;
import com.tsingteng.cosfun.ui.message.comment.usercomment.UserCommentFragment;
import com.tsingteng.cosfun.ui.message.comment.videocomment.VideoCommentFragment;
import com.tsingteng.cosfun.utils.AppUtils;
import com.tsingteng.cosfun.utils.LogonUtils;
import com.tsingteng.cosfun.utils.ToastUtils;
import com.tsingteng.cosfun.utils.VideoUtils;
import com.tsingteng.cosfun.widget.MyRecycleView;
import com.tsingteng.cosfun.widget.TitleView;

import butterknife.BindView;


/**
 * 描述 :
 * 创建人: 肖利.
 * 时间:2018/8/7
 * qq(872179966)
 */

public class OtherFragment extends BasePresenterFragment<OtherPresenter, OtherContract.ITherView> implements OtherContract.ITherView, View.OnClickListener,OtherContract.OtherClickListener{
    @BindView(R.id.tv_view)
    TitleView tvView;
    @BindView(R.id.rv_fan_rv)
    MyRecycleView rvFanRv;
    private String funType;
    private int page = 0;
    private String messageType;
    private int workId;
    private int relatedId;
    private CenterThreeNotification.ListBean items;
    private CenterThreeNotification notificationBean;


    public static OtherFragment newInstance(String funType) {
        OtherFragment fragment = new OtherFragment();
        Bundle bundle = new Bundle();
        bundle.putString("funType", funType);
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    protected void getBundle(Bundle bundle) {
        super.getBundle(bundle);
        funType = bundle.getString("funType");
    }

    @Override
    protected void refresh() {
        page = 1;
        mAdapter.getData().clear();
        mPresenter.loginOfficalNotification(funType, page);
    }

    @Override
    protected void initData(View rootView) {
        mPresenter.loginOfficalNotification(funType, page);
        tvView.setIvLeft(R.drawable.back_big_icon);
        View view = LayoutInflater.from(getContext()).inflate(R.layout.item_null_layout, null);
        Button btn_start_shot = view.findViewById(R.id.btn_start_shot);
        TextView tv_null_tips = view.findViewById(R.id.tv_null_tips);
        if (TextUtils.equals(funType, "FAVOR")) {
            tvView.setTitle("赞我");
            AppUtils.setMessageNumber("FAVOR", 0);
            tv_null_tips.setText("还没有被赞过哎，拍个片接受膜拜～");
            btn_start_shot.setOnClickListener(this);
        } else if (TextUtils.equals(funType, "REVIEW")) {
            AppUtils.setMessageNumber("REVIEW", 0);
            tvView.setTitle("评论我");
            btn_start_shot.setVisibility(View.INVISIBLE);
            tv_null_tips.setText("还木有人评论你诶~");
        } else {
            tvView.setTitle("@我的");
            AppUtils.setMessageNumber("POINT", 0);
            tv_null_tips.setText("还木有被@诶~");
            btn_start_shot.setVisibility(View.INVISIBLE);
            btn_start_shot.setOnClickListener(this);
        }
        mAdapter.setEmptyView(view);
        tvView.setOnTitleClickListener(new TitleView.OnTitleClickListener() {
            @Override
            public void onLeft() {
                getActivity().finish();
            }

            @Override
            public void onRight() {

            }

            @Override
            public void onRight2() {

            }
        });

    }

    @Override
    protected OtherPresenter createPresenter() {
        return new OtherPresenter();
    }

    @Override
    protected View initHeader() {
        return null;
    }

    @Override
    protected MyRecycleView createRecycleView() {
        return rvFanRv;
    }

    @Override
    protected OtherAdapter createAdapter() {
        OtherAdapter otherAdapter=new OtherAdapter();
        otherAdapter.setOtherClickListener(this);
        return otherAdapter;
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        CenterThreeNotification.ListBean listBean = (CenterThreeNotification.ListBean) mAdapter.getData().get(position);
        String relatedType = listBean.getWorkType();
        mPresenter.loginReadCount(funType, notificationBean.getList().get(position).getId());
        items=listBean;
        workId = listBean.getWorkId();
        relatedId = listBean.getRelatedId();
        messageType="item";
        if (TextUtils.equals(funType, "FAVOR")) {
            if (TextUtils.equals(listBean.getWorkType(),"VIDEO")) {
                Intent intent = new Intent(getContext(), PlayActivity.class);
                intent.putExtra("type", 3);
                intent.putExtra("id", listBean.getWorkId());
                intent.getIntExtra("position", 0);
                getContext().startActivity(intent);
            }
            if (TextUtils.equals(listBean.getWorkType(),"PARAS")) {
                messageType="";
                mPresenter.getPlayDetailData(String.valueOf(listBean.getWorkId()));
            }

        }else {
            if (relatedType.equals("PARAS")) {
                mPresenter.getPlayDetailData(String.valueOf(workId));
            } else if (relatedType.equals("VIDEO")) {
                VideoCommentFragment videoCommentFragment = new VideoCommentFragment();
                Bundle bundle = new Bundle();
                bundle.putInt("workId", workId);
                bundle.putInt("commonId", relatedId);
                videoCommentFragment.setArguments(bundle);
                videoCommentFragment.show(getActivity().getSupportFragmentManager(), "userCommentFragment");
            }
            ImageView iv_other_isread = view.findViewById(R.id.iv_other_isread);
            iv_other_isread.setVisibility(View.INVISIBLE);
        }

    }

    @Override
    public void onLoadMoreRequested() {
        page++;
        mPresenter.loginOfficalNotification(funType, page);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_fans;
    }


    @Override
    public void getOtherData(CenterThreeNotification notificationBean) {
        this.notificationBean = notificationBean;
        if (rvFanRv != null) rvFanRv.refreshComplete();
        if (notificationBean == null || notificationBean.getList() == null || notificationBean.getList().size() == 0)
            return;
        mAdapter.addData(notificationBean.getList());
        mAdapter.notifyDataSetChanged();
        if (notificationBean == null) {
            mAdapter.loadMoreComplete();
        } else {
            mAdapter.loadMoreEnd();
        }
    }

    @Override
    public void getReadCountResult(Integer integer) {
        Log.e("isRead", integer + "");
    }

    @Override
    public void showFail(String msg) {
//        super.showFail(msg);
        Navigate.gotoAbNormal(getActivity(),"","PARAS");
    }

    @Override
    public void showOtherDetailsData(PlayDetailsBean playDetailsBean) {
        if (TextUtils.isEmpty(messageType)){
            int relatedId = items.getWorkId();
            Navigate.startPlayDetails(getActivity(), relatedId,"",items.getSource().getId(), 0);
        }else {
            UserCommentFragment userCommentFragment = new UserCommentFragment();
            Bundle bundle = new Bundle();
            bundle.putInt("wid", workId);
            bundle.putInt("rid", relatedId);
            userCommentFragment.setArguments(bundle);
            userCommentFragment.show(getActivity().getSupportFragmentManager(), "userCommentFragment");
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_start_shot:
                VideoHelper.getIntence().starVideoShoot(getActivity());
                break;
        }
    }

    @Override
    public void otherClick(CenterThreeNotification.ListBean item) {
        this.items=item;
        messageType="";
        mPresenter.getPlayDetailData(String.valueOf(item.getWorkId()));
    }
}
